package com.xiaochong.tir.apiservice.scheduled;

import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.apiservice.config.ExecutorConfig;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.apiservice.service.HomeIndexKlineService;
import com.xiaochong.tir.common.data.po.KLineBroaderIndex;
import com.xiaochong.tir.common.util.DateUtils;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.function.BiConsumer;

/**
 * @Project : xc-tir
 * @Package Name : com.xiaochong.tir.apiservice.scheduled
 * @Description : TODO 大盘相关定时任务
 * @Author : daizhiqing@xiaochong.com
 * @Creation Date : 2018年06月27日上午10:22
 * @ModificationHistory Who When What
 */
@Component
public class HomeIndexScheduled {

    @Autowired
    private HomeIndexKlineService homeIndexKlineService;

    @Autowired
    private RedisServer redisServer;

    /**
     * 生成大盘的历史的K线(日K)记录 30天
     */
    @Async
    @Scheduled(fixedRate = 1000*60*60*365)
    public void initHistoryKLineBroaderIndex(){
        LogTrace.beginTrace();

        //获取当前的日期时间
        LocalDateTime dateTime = DateUtils.dateToLocalDateTime(new Date());

        LogTrace.info("计算大盘历史K线" , "date" , dateTime.toString());

        for (int i = 1; i <= 30; i++) {
            //获取计算的时间
            LocalDateTime dateCalculate = dateTime.minusDays(i);
            String dateStr = DateUtils.dateFormatString(DateUtils.localDateTimeToDate(dateCalculate), DateUtils.format_ymds);
            homeIndexKlineService.createBroaderDayKline(dateStr);
        }
        LogTrace.info("计算大盘历史K线完成" , "date" , new Date().toString());

        LogTrace.endTrace();
    }

    /**
     * 每天计算大盘Ma均线 + 大盘K线
     * 每天晚上凌晨00：10分开始计算
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron = "0 10 0 * * ?")
    public void initMaData(){
        LogTrace.beginTrace();

        //获取当前的日期时间
        LocalDateTime dateTime = DateUtils.dateToLocalDateTime(new Date());
        //获取前一天的大盘K线
        LocalDateTime dateCalculate = dateTime.minusDays(1);
        String dateStr = DateUtils.dateFormatString(DateUtils.localDateTimeToDate(dateCalculate), DateUtils.format_ymds);
        KLineBroaderIndex kLineBroaderIndex = homeIndexKlineService.createBroaderDayKline(dateStr);
        LogTrace.info("计算大盘历史K线完成" , "date" , dateStr);

        BigDecimal ma7 = homeIndexKlineService.getMaDate(7);
        redisServer.set(RedisKeysUtils.Broader_DATA_MA_7 , ma7);
        LogTrace.info("_7天大盘Ma均线" , "value" , ma7+"");

        BigDecimal ma30 = homeIndexKlineService.getMaDate(30);
        redisServer.set(RedisKeysUtils.Broader_DATA_MA_30 , ma30);
        LogTrace.info("_30天大盘Ma均线" , "value" , ma30+"");

        BigDecimal ma60 = homeIndexKlineService.getMaDate(60);
        redisServer.set(RedisKeysUtils.Broader_DATA_MA_60 , ma60);
        LogTrace.info("_60天大盘Ma均线" , "value" , ma60+"");
//
//        BigDecimal ma130 = homeIndexKlineService.getMaDate(130);
//        redisServer.set(RedisKeysUtils.Broader_DATA_MA_130 , ma130);
//        LogTrace.info("_130天大盘Ma均线" , "value" , ma130+"");

        String msgLast7 = "";
        String msgLast30 = "";
        if(kLineBroaderIndex != null){
            BigDecimal close = new BigDecimal(kLineBroaderIndex.getClosePrice());
            //收盘指数>=ma7、且收盘指数>=ma30，则：近7天大盘处于走强趋势中
            if(close.compareTo(ma7)>=0 && close.compareTo(ma30)>=0){
                msgLast7 = "近7天大盘处于走强趋势中";
            }
            //收盘指数>=ma7、且收盘指数<ma30，则：近7天大盘反弹未突破
            if(close.compareTo(ma7)>=0 && close.compareTo(ma30)<0){
                msgLast7 = "近7天大盘反弹未突破";
            }
            //收盘指数<ma7、且收盘指数>=ma30，则：近7天大盘下跌有支撑
            if(close.compareTo(ma7)<0 && close.compareTo(ma30)>=0){
                msgLast7 = "近7天大盘下跌有支撑";
            }
            //收盘指数<ma7、且收盘指数<ma30，则：近7天大盘处于走弱趋势中
            if(close.compareTo(ma7)<0 && close.compareTo(ma30)<0){
                msgLast7 = "近7天大盘处于走弱趋势中";
            }


            //若过去一天 收盘指数>=ma30、且收盘指数>=ma60，则：1个月大盘处于走强趋势中
            if(close.compareTo(ma30)>=0 && close.compareTo(ma60)>=0){
                msgLast30 = "1个月大盘处于走强趋势中";
            }
            //若过去一天 收盘指数>=ma30、且收盘指数<ma60，或者过去一天 收盘指数<ma30、且收盘指数>=ma130，则：近1个月大盘处于盘整
            if((close.compareTo(ma30)>=0 && close.compareTo(ma60)<0) || (close.compareTo(ma30)<0 && close.compareTo(ma60)>=0)){
                msgLast30 = "近1个月大盘处于盘整";
            }
            //若过去一天 收盘指数<ma30、且收盘指数<ma60，则：近1个月大盘处于走弱趋势中
            if(close.compareTo(ma30)<0 && close.compareTo(ma60)<0){
                msgLast30 = "近1个月大盘处于走弱趋势中";
            }

            redisServer.set(RedisKeysUtils.BROADER_DATA_7_STATUS , msgLast7);
            redisServer.set(RedisKeysUtils.BROADER_DATA_30_STATUS , msgLast30);
        }
        LogTrace.endTrace();
    }



}
